/***
 * Copyright (c) 2010 readyState Software Ltd
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package net.kts.attraction.search.overlay;



import net.kts.attraction.search.model.LocationFavorite;
import net.kts.attraction.search.sqlite.AttractionFavoriteDaoSQLite;
import net.kts.define.SQLiteDatabaseStr;
import net.kts.main.R;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.OverlayItem;

/**
 * A view representing a MapView marker information balloon.
 * <p>
 * This class has a number of Android resource dependencies:
 * <ul>
 * <li>drawable/balloon_overlay_bg_selector.xml</li>
 * <li>drawable/balloon_overlay_close.png</li>
 * <li>drawable/balloon_overlay_focused.9.png</li>
 * <li>drawable/balloon_overlay_unfocused.9.png</li>
 * <li>layout/balloon_map_overlay.xml</li>
 * </ul>
 * </p>
 * 
 * @author Jeff Gilfelt
 *
 */
public class StartDestOverlayView extends FrameLayout {

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;
	private TextView distanceTxtView = null;
	
	private LocationFavorite LocationFavorite = null;
	private Handler uiCallback = null;
	private Context context = null;

	/**
	 * Create a new BalloonOverlayView.
	 * 
	 * @param context - The activity context.
	 * @param balloonBottomOffset - The bottom padding (in pixels) to be applied
	 * when rendering this view.
	 */
	public StartDestOverlayView(final Context context, int balloonBottomOffset) {

		super(context);
		
		this.context = context;
		
		uiCallbackInit();

		setPadding(10, 0, 10, balloonBottomOffset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(R.layout.attraction_search_dest_overlay, layout);
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
		this.distanceTxtView = (TextView)v.findViewById(R.id.attractionDestDistance);

		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
		ImageView favorite = (ImageView) v.findViewById(R.id.favotie_img_button);
		
		close.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				layout.setVisibility(GONE);
			}
		});
		
		
		favorite.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				uiCallback.sendEmptyMessage(0);
			}
		});
		

		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
				android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;

		addView(layout, params);

	}
	
	private void uiCallbackInit() {
		// TODO Auto-generated method stub
		this.uiCallback = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				
				AttractionFavoriteDaoSQLite attractionFavoriteDaoSQLite = new AttractionFavoriteDaoSQLite(context, SQLiteDatabaseStr.dbFileName, null, 1);
				
				attractionFavoriteDaoSQLite.insertFavorite(LocationFavorite);
				
				Log.d("kts", "insert favorite data");
				
				attractionFavoriteDaoSQLite.close();
				
				Toast.makeText(context, LocationFavorite.getLocationName() +" saved!", Toast.LENGTH_SHORT).show();
				
			}
			
		};
	}

	/**
	 * Sets the view data from a given overlay item.
	 * 
	 * @param item - The overlay item containing the relevant view data 
	 * (title and snippet). 
	 */
	public void setData(OverlayItem item) {
		
		if(this.LocationFavorite == null) {
			this.LocationFavorite = new LocationFavorite();
		}
		
		String tempName = item.getSnippet();
		
		int iGubunX = tempName.indexOf("||");
		if(iGubunX > 0) {
			tempName = tempName.substring(0, iGubunX);
		}	
		
		this.LocationFavorite.setLocationName(tempName);

		this.LocationFavorite.setLocationNameKr(item.getTitle());
		
		this.LocationFavorite.setLatitude(item.getPoint().getLatitudeE6() * 0.000001);
		this.LocationFavorite.setLongitude(item.getPoint().getLongitudeE6()* 0.000001);
		
		
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			title.setText(item.getTitle());
			
			
			
		} else {
			title.setVisibility(GONE);
		}
		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			this.distanceTxtView.setVisibility(VISIBLE);
			
			Log.d("kts", "snippet : " + item.getSnippet() );
			
			String str = item.getSnippet();
			
			int iGubun = str.indexOf("||");
			
			String trans = str.substring(0, iGubun);
			String distance = str.substring(iGubun+2);
			
			//String[] temps = item.getSnippet().split("||");
			snippet.setText(trans);
			this.distanceTxtView.setText("Distance : " + distance + " M");
			
			//snippet.setText(item.getSnippet());
			
			
		} else {
			snippet.setVisibility(GONE);
		}
		
	}
	
	

}
